﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;
using Microsoft.SqlServer.Server;
using Utility;

namespace StudentAppointmentPlatform
{
    public partial class ServiceModify : System.Web.UI.Page
    {
        private AppointmentService service;
        private string loginType;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginType = Session["LoginType"] as string;
            if (string.IsNullOrWhiteSpace(loginType) || loginType != "Director")
            {
                Alert.AlertOnPage(Page, "登录状态不正常！");
                Redirect.Js_Redirect(Page, "/ShowServices.aspx");
            }

            string id = Request.QueryString["id"];
            service = ServiceBO.GetServiceByServiceId(id);

            if (service == null || string.IsNullOrWhiteSpace(service.ServiceName))
            {
                Alert.AlertOnPage(Page, "未找到此服务！");
                Redirect.Js_Redirect(Page, "/ShowServices.aspx");
            }

            if(!IsPostBack)
            {
                tbDirectorName.Text = UserBO.GetUserNameById(service.DirectorId);
                tbServiceName.Text = service.ServiceName;
                tbIntroduction.Text = service.Introduction;
                tbTimeBuckets.Text = service.TimeBuckets;
                tbMultiNum.Text = service.MultiNum.ToString();
                tbPerMultiTime.Text = service.PerMultiTime.ToString();

                if ((bool)service.IsActive)
                {
                    rbActive.Checked = true;
                }
                else
                {
                    rbInactive.Checked = true;
                }
            }
        }

        protected void btnBack_OnClick(object sender, EventArgs e)
        {
            Redirect.Js_Redirect2(Page, "/ShowServices.aspx");
        }

        protected void btnCreateService_OnClick(object sender, EventArgs e)
        {
            service.ServiceName = tbServiceName.Text;
            service.Introduction = tbIntroduction.Text;
            service.MultiNum = int.Parse(tbMultiNum.Text);
            if (rbActive.Checked)
            {
                service.IsActive = true;
            }
            else if (rbInactive.Checked)
            {
                service.IsActive = false;
            }

            if (ServiceBO.UpdateService(service) > 0)
            {
                Alert.AlertOnPage2(Page, "修改成功！");
                Redirect.Js_Redirect2(Page, "/ShowServices.aspx");
            }
        }
    }
}